// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html


export default class ParticalUtil{

    private static _inst:ParticalUtil;

    pool:cc.NodePool;
    
    private constructor()
    {
        this.pool=new cc.NodePool();
    }

    static getInst():ParticalUtil
    {
        if(!this._inst)
        {
            this._inst = new ParticalUtil();
        }
        return this._inst;
    }


   
    addParticle(prefab,parentNode,nodePos,isPlay=true){
        var node;
        if (this.pool.size() > 0) {
            node= this.pool.get();
        } else {
            node = cc.instantiate(prefab);
        }
        node.setPosition(nodePos.x, nodePos.y);
        parentNode.addChild(node);
        var particleSystem=node.getComponent(cc.ParticleSystem);
        //  particleSystem.autoRemoveOnFinish = true;
        if(isPlay){
            particleSystem.resetSystem();
        }
        var t=0;
        particleSystem.lateUpdate=(dt)=>{
            t+=dt;
            if(particleSystem.particleCount===0&&t>0.5){
                this.pool.put(node);
            }
        }
        return particleSystem;
    }
 
    // update (dt) {}
}

 